package com.hcbd.tdwt.ui.common.adapter;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.hcbd.tdwt.R;

import java.util.List;

public class MyBluetoothAdapter extends RecyclerView.Adapter<MyBluetoothAdapter.MyViewHolder>{

    protected final Context mContext;
    private List<BluetoothDevice> blueToothList;
    private final LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    public MyBluetoothAdapter(Context context, List<BluetoothDevice> list){
        mContext=context;
        mInflater=((Activity)context).getLayoutInflater();
        blueToothList=list;

    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
        View view=mInflater.inflate(R.layout.bluetooth_item,viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder,int i){
        BluetoothDevice bluetoothDevice =blueToothList.get(i);
        myViewHolder.bluetoothName.setText(bluetoothDevice.getName());
        myViewHolder.adders.setText(bluetoothDevice.getAddress());
        myViewHolder.areaLL.setOnClickListener(v -> onItemClickListener.onClick(bluetoothDevice));
    }

    @Override
    public int getItemCount(){
        return blueToothList.size();
    }

    public void notifyDataSetChanged(List<BluetoothDevice> list){
        blueToothList=list;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView bluetoothName,adders;
        RelativeLayout areaLL;
        MyViewHolder(View itemView) {
            super(itemView);
            bluetoothName = itemView.findViewById(R.id.bluetoothName);
            adders = itemView.findViewById(R.id.adders);
            areaLL=itemView.findViewById(R.id.areaLL);
        }
    }

    public interface OnItemClickListener {
        void  onClick(BluetoothDevice bluetoothDevice);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
